package com.matchmaking.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.matchmaking.system.domain.*;
import com.matchmaking.system.mapper.*;
import com.matchmaking.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author 卟哭！
 * @Date 2024/03/21  15:40
 * @Version 1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysPostMapper sysPostMapper;
    @Autowired
    private SysUserPostMapper sysUserPostMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;


    @Override
    public List<SysUser> selectObjects() {

        return sysUserMapper.selectObjects(new SysUser());
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @Override
    public List<SysUser> selectObjects(SysUser entity) {
        return sysUserMapper.selectObjects(entity);
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public SysUser selectById(Integer id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if (sysUser != null) {
            //查询用户岗位
            List<SysUserPost> sysUserPosts = sysUserPostMapper.selectObjectsByUserId(sysUser.getUserId());
            List<Long> postIds = sysUserPosts.stream().map(SysUserPost::getPostId).collect(Collectors.toList());
            sysUser.setPostIds(postIds.toArray(new Long[0]));
        }
        List<SysRole> roles = sysUser.getRoles();
        List<Long> roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
        sysUser.setRoleIds(roleIds.toArray(new Long[0]));
        return sysUser;
    }

    /**
     * 新增用户
     *
     * @param entity
     * @return
     */
    @Transactional
    @Override
    public boolean insertObject(SysUser entity) {
        //新增用户
        sysUserMapper.insertObject(entity);
        Long[] postIds = entity.getPostIds();
        Long[] roleIds = entity.getRoleIds();
        //新增用户和岗位关系
        for (Long postId : postIds) {
            SysUserPost sysUserPost = new SysUserPost();
            sysUserPost.setUserId(entity.getUserId());
            sysUserPost.setPostId(postId);
            sysUserPostMapper.insertObject(sysUserPost);
        }
        //新增用户和角色关系
        for (Long roleId : roleIds) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(entity.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insertObject(sysUserRole);
        }
        return true;
    }

    /**
     * 修改用户信息
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateObjest(SysUser entity) {
        //修改用户
        sysUserMapper.updateObjest(entity);
        Long[] postIds = entity.getPostIds();
        Long[] roleIds = entity.getRoleIds();
        //删除原有用户和岗位关系
        sysUserPostMapper.deleteObject(entity.getUserId());
        //新增用户和岗位关系
        for (Long postId : postIds) {
            SysUserPost sysUserPost = new SysUserPost();
            sysUserPost.setUserId(entity.getUserId());
            sysUserPost.setPostId(postId);
            sysUserPostMapper.insertObject(sysUserPost);
        }
        //删除原有用户和角色关系
        sysUserRoleMapper.deleteObject(entity.getUserId());
        //新增用户和角色关系
        for (Long roleId : roleIds) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(entity.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insertObject(sysUserRole);
        }
        return true;
    }


    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean deleteObjest(Long id) {
        //删除用户
        sysUserMapper.deleteObjest(id);
        //删除用户和岗位关系
        sysUserPostMapper.deleteObject(id);
        //删除用户和角色关系
        sysUserRoleMapper.deleteObject(id);

        return true;
    }

    @Transactional
    @Override
    public boolean deleteObjests(Long[] ids) {
        //批量删除用户
        int i = sysUserMapper.deleteObjests(ids);
        if (i > 0) {
            //批量删除用户和岗位关系
            sysUserPostMapper.BatchdeleteObjects(ids);
            //批量删除用户和角色关系
            sysUserRoleMapper.BatchdeleteObjects(ids);
        }
        return true;
    }

    /**
     * 查询角色列表
     *
     * @return
     */
    @Override
    public List<SysRole> selectRoles() {
        return sysRoleMapper.selectAll();
    }

    /**
     * 查询岗位列表
     *
     * @return
     */
    @Override
    public List<SysPost> selectPosts() {
        return sysPostMapper.selectObjects();
    }

    @Override
    public List<Tree<String>> selectDeptsTree() {
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setWeightKey("orderNum");
        treeNodeConfig.setIdKey("deptId");
        treeNodeConfig.setNameKey("deptName");
        treeNodeConfig.setParentIdKey("parentId");
        List<SysDept> sysDepts = sysDeptMapper.selectObjects();
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(sysDepts, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getDeptId() + "");
                    tree.setParentId(treeNode.getParentId() + "");
                    tree.setWeight(treeNode.getOrderNum());
                    tree.setName(treeNode.getDeptName());
                    //暂时展示以下字段
                    tree.putExtra("ancestors", treeNode.getAncestors());
                    tree.putExtra("status", treeNode.getStatus());
                });
        return treeNodes;
    }

    /**
     * 修改用户状态
     * @param user
     * @return
     */
    @Override
    public boolean updateStatus(SysUser user) {
        return sysUserMapper.updateObjest(user) > 0;
    }


}
